package cn.tl.org.controller;

import cn.tl.org.domain.Dept;
import cn.tl.org.query.DeptQuery;
import cn.tl.org.service.IDeptService;
import cn.tl.org.service.IEmployeeService;
import cn.tl.org.utils.AjaxResult;
import cn.tl.org.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门维护")
//@CrossOrigin
public class DeptController {
    @Autowired
    private IDeptService deptService;
    @PutMapping
    @ApiOperation(value = "新增或修改接口")
    public AjaxResult saveOrUpt(@RequestBody Dept dept){
        try {
            if (dept.getId() == null){
                deptService.save(dept);
            }else {
                deptService.update(dept);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            deptService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Dept findById(@PathVariable("id") Long id){
       return deptService.findById(id);
    }
    @GetMapping
    public List<Dept> findAll(){
        return deptService.findAll();
    }

    @PostMapping
    public QueryResult<Dept> queryPage(@RequestBody DeptQuery deptQuery){
        return deptService.queryPage(deptQuery);
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            deptService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/deptTree")
    public List<Dept> loadDeptTree(){
        return deptService.loadDeptTree();
    }
}
